package com.example.onlineeducation.repository

import com.example.onlineeducation.data.constant.AccountType
import com.example.onlineeducation.data.constant.Constants
import com.example.onlineeducation.data.db.datastore.UserInfoDataStoreKey
import com.example.onlineeducation.data.entity.LoginUserInfoEntity
import com.example.onlineeducation.data.json.LoginJson
import com.example.onlineeducation.data.json.UserInfoJson
import com.example.onlineeducation.mapper.GetUserInfoMapper
import com.example.onlineeducation.net.ApiService
import com.yc.jetpacklib.base.YcRepository
import com.yc.jetpacklib.extension.ycFlow
import com.yc.jetpacklib.extension.ycToRequestBody
import com.yc.jetpacklib.net.YcResult
import kotlinx.coroutines.flow.Flow
import org.json.JSONException
import org.json.JSONObject
import ycGetDataNoNull

/**
 * SimpleDes:
 * Creator: Sindi
 * Date: 2022-04-01
 * UseDes:
 */
class UserRepository(private val apiService: ApiService) : YcRepository() {
    private val mGetUserInfoMapper by lazy { GetUserInfoMapper() }

    /**
     * 发送验证码
     */
    fun sendVerifyCode(phoneNum: String): Flow<YcResult<String>> = ycFlow {
        val jsonData = apiService.sendCode(phoneNum).ycGetDataNoNull()
        send(YcResult.Success(jsonData))
    }

    /**
     * 登录
     */
    fun login(phoneNum: String, verifyCode: String): Flow<YcResult<LoginJson>> = ycFlow {
        UserInfoDataStoreKey.clearLoginState()
        val requestData = JSONObject()
        try {
            requestData.put("mobile", phoneNum)
            requestData.put("captcha", verifyCode)//短信验证码
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        val requestBody = requestData.toString().ycToRequestBody()
        val jsonData = apiService.login(requestBody).ycGetDataNoNull()
        UserInfoDataStoreKey.saveLoginState(phoneNum, jsonData.token, jsonData.userInfo?.username)
        send(YcResult.Success(jsonData))
    }

    /**
     * 注册
     */
    fun register(name: String, phone: String,idCard: String, sex: String
                 ,projId: String, groupId: String,personTypeId: String, dutyName: String): Flow<YcResult<String>> = ycFlow {
        UserInfoDataStoreKey.clearLoginState()
        val requestData = JSONObject()
        try {
            requestData.put("name", name)
            requestData.put("phone", phone)
            requestData.put("idCard", idCard)
            requestData.put("sex", sex)
            requestData.put("projId", projId)
            requestData.put("groupId", groupId)
            requestData.put("personTypeId", personTypeId)
            requestData.put("dutyName", dutyName)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        val requestBody = requestData.toString().ycToRequestBody()
        val jsonData = apiService.register(requestBody).ycGetDataNoNull()
        send(YcResult.Success(jsonData))
    }

    /**
     * 获取用户信息
     */
    fun getUserInfo(): Flow<YcResult<LoginUserInfoEntity>> = ycFlow {
        val jsonData = apiService.getUserInfo().ycGetDataNoNull()
        send(YcResult.Success(mGetUserInfoMapper.map(jsonData)))
    }

    /**
     * 登出
     */
    fun logout(): Flow<YcResult<String?>> = ycFlow {
        val jsonData = apiService.logout()
        UserInfoDataStoreKey.clearLoginState()
        send(YcResult.Success(jsonData.data))
    }
}